package com.ljx.itbbs.server.article.dao;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.itbbs.server.article.domain.entity.ArticleCategoryEntity;
import com.ljx.itbbs.server.article.domain.vo.resp.ArticleCategoryResp;
import com.ljx.itbbs.server.article.mapper.ArticleCategoryMapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleCategoryDao extends ServiceImpl<ArticleCategoryMapper, ArticleCategoryEntity> {

    public List<ArticleCategoryResp> getAllCategory() {
        return BeanUtil.copyToList(this.baseMapper.selectList(
                new LambdaQueryWrapper<ArticleCategoryEntity>().orderByAsc(ArticleCategoryEntity::getSort)
        ), ArticleCategoryResp.class);
    }

    public void addArticleCategoryNum(Long articleCategoryId) {
        lambdaUpdate().eq(ArticleCategoryEntity::getId, articleCategoryId)
                .setSql("article_num = article_num + 1")
                .update();
    }
}
